/**
 * 商品分类路由
 */

const express = require('express');
const router = express.Router();
const { networkStatus } = require('../utils/constant');
const { addProductType, readProductType, deleteProductType, updateProductType, queryProductType } = require('../file/productType');


/**
 * 添加商品分类
 * @param {*} data 
 * @returns 
 */
router.post('/add', async (req, res) => {
    console.log('body', req.body);
    try {
        await addProductType(req.body);
        res.send({
            code: 200,
            data: null,
            msg: networkStatus['200'],
        })
    }catch (error) {
        console.log(error);
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

/**
 * 查询所有商品分类
 * @param {*} data 
 * @returns 
 */
router.post('/findAll', async (req, res) => {
    console.log('body', req.body);
    try {
        const data =  await readProductType(req.body);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    }catch (error) {
        console.log(error);
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

// 删除商品分类
router.post('/delete', async (req, res) => {
    try {
        const data = await deleteProductType(req.query);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    } catch (error) {
        res.send({
            code: 500,
            data: error,
            msg: error ? error : networkStatus['500'],
        })
    }
})

// 修改商品分类
router.post('/update', async (req, res) => {
    console.log('body', req.body);
    try {
        await updateProductType(req.body);
        res.send({
            code: 200,
            data: null,
            msg: networkStatus['200'],
        })
    } catch (error) {
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

// 分类查询商品分类
router.post('/query', async (req, res) => {
    console.log('body', req.body);
    try {
        const data = await queryProductType(req.body);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    } catch (error) {
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

module.exports = router;
